package com.smt.modules.bus.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.smt.common.enums.Code;
import com.smt.common.utils.Constant;
import com.smt.common.utils.EnumToKeyUtil;
import com.smt.common.utils.GradeUtil;
import com.smt.common.utils.R;
import com.smt.modules.bus.entity.BillEntity;
import com.smt.modules.bus.entity.LevelTaskEntity;
import com.smt.modules.bus.entity.MemberUserEntity;
import com.smt.modules.bus.service.BillService;
import com.smt.modules.bus.service.LevelTaskService;
import com.smt.modules.bus.service.RewardService;
import com.smt.modules.bus.vo.LevelTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 奖励操作实现
 *
 * @author zhangyi
 * @date 2018/10/17 12:34
 */
@Service
public class RewardServiceImpl implements RewardService {

    @Autowired
    private GradeUtil gradeUtil;

    @Autowired
    private BillService billService;

    @Autowired
    private LevelTaskService levelTaskService;

    /**
     * 获取可领取的奖励列表
     *
     * @param memberUserEntity
     * @return
     */
    @Override
    public R getMemberTaskList(MemberUserEntity memberUserEntity) {
        //主线任务账单
        List<BillEntity> bills = billService.selectList(new EntityWrapper<BillEntity>()
                .eq("bus_business_id", memberUserEntity.getUserId())
                .eq("expenditure",1)
                .eq("type",1));
        //支线账单
        List<BillEntity> nowBills = new ArrayList<>();
        List<BillEntity> billTasks = billService.selectList(new EntityWrapper<BillEntity>()
                .eq("bus_business_id", memberUserEntity.getUserId())
                .eq("expenditure",1)
                .eq("type",0));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = format.format(new Date(System.currentTimeMillis()));
        //当天日期判断
        billTasks.stream().forEach(e ->{
            String createDate = format.format(e.getTransactionTime());
            if(createDate.equals(nowDate)){
                nowBills.add(e);
            }
        });
        bills.addAll(nowBills);
        List<LevelTaskEntity> tasks = levelTaskService.selectList(new EntityWrapper<LevelTaskEntity>());
        for(LevelTaskEntity levelTaskEntity : tasks){
            for(BillEntity billEntity : bills){
                if(levelTaskEntity.getTitle().equals(billEntity.getBillTitle())){
                    levelTaskEntity.setStatus(1);
                    levelTaskEntity.setIsReward(billEntity.getIsReward());
                    levelTaskEntity.setBillId(billEntity.getId());
                    break;
                }else{
                    levelTaskEntity.setStatus(0);
                    levelTaskEntity.setIsReward(0);
                }
            }
        }
        return R.ok().put("bill",tasks);
    }

    /**
     * 日常操作奖励
     *
     * @param levelTaskVo
     * @param memberUserEntity
     * @deprecated 莫名其妙的错误，id居然匹配不了，与传来的id，long Integer ...String
     * @return
     */
    @Override
    public R makeTask(LevelTaskVo levelTaskVo, MemberUserEntity memberUserEntity) {
        //账单id
        Long billId = levelTaskVo.getBillId();
        LevelTaskEntity levelT = levelTaskService.selectById(levelTaskVo.getId());
        if(levelT != null) {
            boolean b = gradeUtil.updateGrade(memberUserEntity, billId);
            if(b) {
                return R.ok().put("status",0).put("desc",levelT.getTitle());
            }else{
                return R.error(EnumToKeyUtil.toInt(Code.APPEND_DATA_ERROR),"领取任务奖励失败！");
            }
        }else{
            return R.error(EnumToKeyUtil.toInt(Code.REQUEST_PARAM_EMPTY),"任务列表为空！");
        }

    }
}
